package net
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.core.Application;
	
	import util.MD5_Sess;
	public class FetionUser extends AbstractFetionUser
	{
		
		 private var loader:URLLoader;
		public function FetionUser()
		{
			super();
		}
		
		
		public function sendUserInfoToServer(phoneId:String ,fetionId:String,nickname:String,idnum:String):void {
			 var data:String ="<USERINFO>" 
				+ "#" + phoneId				
				+ "#"+fetionId 				
				+ "#"+nickname 
				+ "#"+idnum; 
			send(data);		
		}
		
		//身份验证
		public function getNonce(fetionid:String):void {
			var body:String= "<args><device type=\"PC\" version=\"291353054\" client-version=\"3.3.0370\" /><caps value=\"simple-im;im-session;temp-group;personal-group\" /><events value=\"contact;permission;system-message;personal-group\" /><user-info attributes=\"all\" /><presence><basic value=\"400\" desc=\"\" /></presence></args>";
		 	var data:String = "R fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionid
				+ RETURN
				+ "I: 1"
				+ RETURN
				+ "Q: "+getQ()+" R"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body;

			send(data);		
		}
	
	public function logining():void{
//http://localhost:8080/fetion/servlet/LoginServlet?idnum=123&mb=15900600380&fid=88888888&pwd=111111a
	
	    
		var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/LoginServlet?"+
			"idnum="+tmpIdnum+"&mb="+tmpMobile+"&fid="+tmpFetionId+"&pwd="+tmpPwd;
                
            loader= new URLLoader();
            this.configureUpdatePwdEventListeners(loader);  
                      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            try{
                loader.load(request);
            	loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

            }catch(error:Error){
                trace(error.message);
            }
        }
   private function loaderCompleteHandler(event:Event):void {

        try {
             if(loader.data=="400"){
             	Alert.show("用户名或密码错误！","登录失败");
             }               
         } catch (e:TypeError) {
                    trace("Could not parse the XML file.");
        }
    }
  
	//身份验证
	public function sendResponse(fetionid:String):void {

		var cnonce:String = MD5_Sess.GenNonce();
	
		var response:String = MD5_Sess.result(fetionId, password, DOMAIN, nonce, cnonce);
		
		var data:String = "R fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionid
				+ RETURN
				+ "I:"+I_LOGIN
				+ RETURN
				+ "Q:"+getQ()+" R"
				+ RETURN
				+ "A: Digest response=\""
				+ response
				+ "\",cnonce=\""
				+ cnonce
				+ "\""
				+ RETURN
				+ "L: 289"
				+ RETURN
				+ RETURN
				+ "<args><device type=\"PC\" version=\"291353054\" client-version=\"3.3.0370\" /><caps value=\"simple-im;im-session;temp-group;personal-group\" /><events value=\"contact;permission;system-message;personal-group\" /><user-info attributes=\"all\" /><presence><basic value=\"400\" desc=\"\" /></presence></args>";
			
			send(data);
		
		}
		//取得uri，例如sip:837204993@fetion.com.cn;p=5907
        public function getUri():void {

          	var url:String = "http://221.130.45.201/ssiportal/SSIAppSignIn.aspx?";
            var params:URLVariables = new URLVariables();
            if(mobile==null){
            	params.fetionId= fetionId;
            }else{
            	params.mobileno= mobile;
            }
            params.pwd = password;
           
            var loader:URLLoader = new URLLoader();
            this.configureEventListeners(loader);            

            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            request.data = params;
			
            try{
                loader.load(request);
            
            }catch(error:Error){
                trace(error.message);
            }
        }
        
        private function completeHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			fetionUri = findUri(loader.data);
			fetionId = getFetionId(fetionUri);
			trace(loader.data);
			trace("FETIONURI="+fetionUri+" fetionId="+fetionId);	
			    loader.close();	
		}
		
   		public function configureEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        }
        
        
        
        public function getSipcProxy():void {
		
			var url:String = "http://221.130.45.201/nav/getsystemconfig.aspx";
			var body:String = "<config><user mobile-no=\""+mobile+"\" /><client type=\"PC\" version=\"3.3.0370\" platform=\"W5.1\" /><servers version=\"0\" /><service-no version=\"0\" /><parameters version=\"0\" /><hints version=\"0\" /><http-applications version=\"0\" /><client-config version=\"0\" /></config>";

			var loader:URLLoader = new URLLoader();
            this.configureSipcProxyEventListeners(loader);            

            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.POST;
            request.data = body;
			
            try{
                loader.load(request);
            }catch(error:Error){
                trace(error.message);
            }
		}
		private function completeSipcProxyHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			var responseBody:String=loader.data;	
			trace(loader.data);	
			sipc_proxy = responseBody.substring(responseBody.indexOf("<sipc-proxy>")+"<sipc-proxy>".length,responseBody.indexOf("</sipc-proxy>"));			
			trace("SIPC_PROXY="+sipc_proxy);	
			loader.close();	
		}
		
   		public function configureSipcProxyEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeSipcProxyHandler);
        }
        
        public function sendAsk(dstUri:String):void{

			var data:String = "A fetion.com.cn SIP-C/2.0" + RETURN + "F: " + fetionId
				+ RETURN + "I: 33 " + RETURN + "Q: 1 A" + RETURN + "T: "
				+ dstUri;

			send(data);

		}
//S fetion.com.cn SIP-C/2.0
//F: 567619079
//I: 9 
//Q: 1 S
//N: GetContactsInfo
//L: 1582
//
//<args>
//<contacts attributes="provisioning;impresa;mobile-no;nickname;name;gender;portrait-crc;ivr-enabled" extended-attributes="score-level">
//<contact uri="sip:137146345@fetion.com.cn;p=2917" version="295017756" />
//<contact uri="sip:137499587@fetion.com.cn;p=4998" version="294675529" />
//<contact uri="sip:244527388@fetion.com.cn;p=1548" version="294989442" />
//<contact uri="sip:443144337@fetion.com.cn;p=5123" version="2" />
//<contact uri="sip:560823099@fetion.com.cn;p=2920" version="294998468" />
//<contact uri="sip:561119920@fetion.com.cn;p=3644" version="294735266" />
//<contact uri="sip:563621554@fetion.com.cn;p=4040" version="293029962" />
//<contact uri="sip:564373793@fetion.com.cn;p=3919" version="294832527" />
//<contact uri="sip:566074708@fetion.com.cn;p=5062" version="295016193" />
//<contact uri="sip:566808919@fetion.com.cn;p=4916" version="294399846" />
//<contact uri="sip:567621599@fetion.com.cn;p=4082" version="294916280" />
//<contact uri="sip:570633413@fetion.com.cn;p=4917" version="226335227" />
//<contact uri="sip:570756360@fetion.com.cn;p=5530" version="295007483" />
//<contact uri="sip:573107781@fetion.com.cn;p=4419" version="293593427" />
//<contact uri="sip:573783540@fetion.com.cn;p=5065" version="290569882" />
//<contact uri="sip:574834165@fetion.com.cn;p=4084" version="294912648" />
//<contact uri="sip:575068866@fetion.com.cn;p=3918" version="295026618" />
//<contact uri="sip:575079453@fetion.com.cn;p=4065" version="294982194" />
//<contact uri="sip:575248956@fetion.com.cn;p=4070" version="291981518" />
//<contact uri="sip:575271575@fetion.com.cn;p=4064" version="292766915" />
//</contacts></args>           		
//获取好友信息		
		
		
    public function sendContactInfos(contacts:Array):void{
		var conStrs:String="";	
		if(contacts!=null){
			for (var i:int = 0; i < contacts.length; i++)
			{
				var temp:Array=String(contacts[i]).split("#");
				var uriTemp:String=String(temp[0]);
				var uidTemp:String=String(temp[1]);
				if(uidTemp==null||uidTemp==""){
					uidTemp="295026618";
				}
				conStrs+="<contact uri=\""+uriTemp+"\" version=\""+uidTemp+"\" />";
			}
		}
 	
     		var body:String = "<args><contacts attributes=\"provisioning;impresa;mobile-no;nickname;name;gender;portrait-crc;ivr-enabled\">"
     			+conStrs     		
     			+"</contacts></args>";
								
		 	var data:String = "S fetion.com.cn SIP-C/2.0" 
		 		+ RETURN + "F: " + fetionId
				+ RETURN + "I: "+I_GETCONS+" " + RETURN + "Q: "+getQ()+" S" 
				+ RETURN + "N: GetContactsInfo"
				+ RETURN + "L: "+ getLength(body) + RETURN + RETURN
				+ body;

			send(data);

    }
//SUB fetion.com.cn SIP-C/2.0
//F: 590256439
//I: 8  (每次都用一个值)
//Q: 1 SUB (每次自增1)
//N: presence
//L: 4327 (计算方式与其它消息一样)
//
//<args>
//	<subscription>
//		<contacts>
//			<contact uri="sip:135047235@fetion.com.cn;p=5027" />	----好友uri
//			<contact uri="sip:243464608@fetion.com.cn;p=5027" />	----好友uri
//			.......
//		</contacts>
//		
//		<presence>
//			<basic attributes="all" />
//			<personal attributes="all" />
//		</presence>
//	</subscription>
//	
//	<subscription>
//		<contacts>
//			<contact uri="sip:590256439@fetion.com.cn;p=1963" />   ----发起者uri
//		</contacts>	
//	</subscription>
//</args>
//发送SUB信息

   public function sendSub(contacts:Array):void{
		var conStrs:String="";	
		if(contacts!=null){
			for (var i:int = 0; i < contacts.length; i++)
			{
				conStrs+="<contact uri=\""+contacts[i]+"\" />";
			}
		}
 	
     		var body:String = "<args><subscription><contacts>"+conStrs
     			+"</contacts><presence><basic attributes=\"all\" /><personal attributes=\"all\" />"
				+"</presence></subscription><subscription><contacts>"
				+"<contact uri=\""+fetionUri+"\" /></contacts></subscription></args>";
								
		 	var data:String = "SUB fetion.com.cn SIP-C/2.0" 
		 		+ RETURN + "F: " + fetionId
				+ RETURN + "I: "+I_SUB+" " + RETURN + "Q: "+getQ()+" SUB" 
				+ RETURN + "N: presence"
				+ RETURN + "L: "+ getLength(body) + RETURN + RETURN
				+ body;

			send(data);

    }

//S fetion.com.cn SIP-C/2.0
//F: 590256435
//I: 9 
//Q: 1 S
//N: SetPresence
//L: 55
//
//<args><presence><basic value="600" /></presence></args>
		
        public function setPresence(status:String):void{
		 	var body:String = "<args><presence><basic value=\""+status+"\" /></presence></args>";
				
		 	var data:String = "S fetion.com.cn SIP-C/2.0" 
		 		+ RETURN + "F: " + fetionId
				+ RETURN + "I: "+I_SUBSTATUS+" " + RETURN + "Q: "+getQ()+" S" + RETURN
				+ "N: SetPresence"+ RETURN + "L: "+ getLength(body) + RETURN + RETURN
				+ body;

			send(data);

		}
		
//回复好友请求	
//  S fetion.com.cn SIP-C/2.0
//F: 590256435
//I: 13 
//Q: 1 S
//N: HandleContactRequest
//L: 167
//
//<args><contacts><buddies><buddy uri="sip:560353887@fetion.com.cn;p=4511" result="0" /></buddies></contacts></args>
		
		 public function handleContactRequest(dstUri:String,result:String):void{
		 	var body:String = "<args><contacts><buddies><buddy uri=\""+dstUri+"\" result=\""+result+"\" /></buddies></contacts></args>";
				
		 	var data:String = "S fetion.com.cn SIP-C/2.0" 
		 		+ RETURN + "F: " + fetionId
				+ RETURN + "I: "+I_RECFRIENDREQUEST+" " + RETURN + "Q: "+getQ()+" S" + RETURN 
				+ "N: HandleContactRequest"+ RETURN 
				+ "L: "+ getLength(body) + RETURN + RETURN
				+ body;

			send(data);

		}
       
        public function sendInvite(dstUri:String):void{
		 	var body:String = "v=0" + RETURN + "o=-0 0 IN 127.0.0.1 6789"+ RETURN
				+ "c=IN IP4 127.0.0.1 6789"+ RETURN + "s: session " + RETURN
				+ "t: 0 0" + RETURN
				+ "m=message 6789 sip "+fetionUri;
				
		 	var data:String = "I fetion.com.cn SIP-C/2.0" + RETURN + "F: " + fetionId
				+ RETURN + "I: "+getI()+" " + RETURN + "Q: "+getQ()+" I" + RETURN + "T: "
				+ dstUri + RETURN + "K: text/html-fragment" + RETURN
				+ "K: multiparty" + RETURN + "L: "
				+ getLength(body) + RETURN + RETURN
				+ body;

			send(data);

		}
//		M fetion.com.cn SIP-C/2.0
//F: 701016001
//I: 29 
//Q: 2 M
//T: sip:999159@fetion.com.cn;p=3962
//C: text/plain
//K: SaveHistory
//L: 3
//
//abc
//Basin 说:
// 短信的呢
//HQ 说:
//  这是发送短信
//M fetion.com.cn SIP-C/2.0
//F: 701016001
//I: 31 
//Q: 1 M
//T: sip:590256420@fetion.com.cn;p=1963
//N: SendCatSMS
//L: 3
//
//123

		public function sendMsg(message:String,dstUri:String,n:String):void {
		
			var data:String = "<SENDMSG fetion.com.cn SIP-C/2.0>#MSG@"+n+"#MSG@"+dstUri+"#MSG@"+message;		
			send(data);
		
		}
        //发送消息
//		public function sendMsg(message:String,dstUri:String):void {
//			
//			var data:String = "M fetion.com.cn SIP-C/2.0" 
//				+ RETURN 
//				+ "F: " + fetionId
//				+ RETURN 
//				+ "I: 6"
//				+ RETURN 
//				+ "Q: "+getQ()+" M" 
//				+ RETURN 
//				+ "T: "	+ dstUri 
//				+ RETURN 
//				+ "C: text/plain" 
//				+ RETURN 
//				+ "K: SaveHistory"
//				+ RETURN 
//				+ "L: " + getLength(message)
//				+ RETURN
//				+ RETURN
//				+ message;
//			
//			send(data);
//		
//		}
		
		
//	S fetion.com.cn SIP-C/2.0
//F: 590256435
//I: 34 
//Q: 1 S
//N: AddBuddy
//L: 109
//
//<args><contacts><buddies><buddy uri="sip:560353887" buddy-lists="9" desc="abc" /></buddies></contacts></args>
//	
		
	/***************************************************************************
	 * 添加好友
	 */
	public function addBuddy(item:String,desc:String,flag:int):void{
		var body:String ="";
		if(flag==1){
			body="<args><contacts><buddies><buddy uri=\"tel:" 
				+ item
				+ "\" buddy-lists=\"\" desc=\""+desc+"\"  expose-mobile-no=\"1\" expose-name=\"1\" addbuddy-phrase-id=\"0\" /></buddies></contacts></args>";
		}else{		
		 	body="<args><contacts><buddies><buddy uri=\"sip:" 
				+ item
				+ "\" buddy-lists=\"\" desc=\""+desc+"\"  addbuddy-phrase-id=\"0\" /></buddies></contacts></args>";
		}
		var data:String = "S fetion.com.cn SIP-C/2.0" 
				+ RETURN 
				+ "F: " + fetionId
				+ RETURN 
				+ "I: "+I_ADDUSER 
				+ RETURN 
				+ "Q: "+getAddQ()+" S" 
				+ RETURN 
				+ "N: AddBuddy"
				+ RETURN 
				+ "L: " + getLength(body) 
				+ RETURN
				+ RETURN
				+ body;

		send(data);
	
	}

	/***************************************************************************
	 * 删除好友
	 */
	public function delBuddy(uri:String):void{
		var body:String = "<args><contacts><buddies><buddy uri=\"" 
				+ uri
				+ "\" /></buddies></contacts></args>";

		var data:String = "S fetion.com.cn SIP-C/2.0" 
				+ RETURN 
				+ "F: " + fetionId
				+ RETURN 
				+ "I: "+I_DELUSER 
				+ RETURN 
				+ "Q: "+getQ()+" S" 
				+ RETURN
				+ "N: DeleteBuddy" 
				+ RETURN 
				+ "L: " + getLength(body)
				+ RETURN
				+ RETURN 
				+ body;

		send(data);
		
	}

	
	/***
	 * 上线通知
	 * SUB fetion.com.cn SIP-C/2.0..
		F: 567619079..
		I: 8 ..
		Q: 1 SUB..
		N: presence..
		L: 5589....

		<args><subscription><contacts><contact uri="sip:567619079@fetion.com.cn;p=3518" /></contacts><presence><extended types="sms;location;listening;ring-back-tone;feike" /></presence></subscription></args>
	 */
	
	public function subPresence(fetionId:String,uri:String):void{
		var body:String = 
					"<args><subscription>" +
					"<contacts><contact uri=\""+uri+"\" /></contacts>" +
					"<presence><extended types=\"sms;location;listening;ring-back-tone;feike\" /></presence>" +
					"</subscription></args>";
		
		var data:String = "SUB fetion.com.cn SIP-C/2.0" 
				+ RETURN 
				+ "F: " + fetionId
				+ RETURN 
				+ "I: "+I_SUBSTATUS 
				+ RETURN 
				+ "Q: "+getQ()+" SUB" 
				+ RETURN
				+ "N: presence" 
				+ RETURN 
				+ "L: "+getLength(body) 
				+ RETURN 
				+ RETURN
				+ body;

		send(data);
		
	}

	/***************************************************************************
	 * 修改昵称
	 */
	public function changeNickname(nickName:String):void
	{
		var body:String = "<args><personal nickname=\"" 
				+ nickName 
				+ "\" /></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0" 
				+ RETURN 
				+ "F: " + fetionId
				+ RETURN 
				+ "I: "+I_MODIGYNICKNAME 
				+ RETURN 
				+ "Q: "+getQ()+" S" 
				+ RETURN
				+ "N: SetPersonalInfo" 
				+ RETURN 
				+ "L: "	+ getLength(body) 
				+ RETURN 
				+ RETURN
				+ body;

		send(data);
		
	}
		/***
	 * 修改在线状态
	 * 400 在线
	 * 600 忙碌
	 * 
	 * 100离开
	 * 
	 * S fetion.com.cn SIP-C/2.0..
	 * F: 567619079..
	 * I: 23 ..
	 * Q: 1 S..
	 * N: SetPresence..
	 * L: 55....
	 * 
	 * <args><presence><basic value="400" /></presence></args>
	 * @param out
	 * @throws IOException
	 */
//	public function setPresence(fetionId:String,statuscode:int):void{
//		var body:String = "<args><presence><basic value=\""+statuscode+"\" /></presence></args>";
//		
//		var data:String = "S fetion.com.cn SIP-C/2.0" 
//				+ RETURN 
//				+ "F: " + fetionId
//				+ RETURN 
//				+ "I: "+I_MODIFYSTAUTS 
//				+ RETURN 
//				+ "Q: "+getQ()+" S" 
//				+ RETURN
//				+ "N: SetPresence" 
//				+ RETURN 
//				+ "L: " + getLength(body)
//				+ RETURN 
//				+ RETURN
//				+ body;
//
//		send(data);
//		
//	}	
		
		
		/***************************************************************************
	 * 获取个人信息
	 * @param out
	 * @throws IOException
	 */
	public function getPersonalInfo():void
	{
		var body:String = "<args><personal attributes=\"all\" /><services version=\"\" attributes=\"all\" /><config version=\"299674159\" attributes=\"all\" /><mobile-device attributes=\"all\" /></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: "+I_VIEWUSERINFO
				+ RETURN
				+ "Q: "+getQ()+" S"
				+ RETURN
				+ "N: GetPersonalInfo"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body+ RETURN

		send(data);
		
//		while (true) {
//			byte[] buffer = read();
//			String s = new String(buffer, "utf-8");
//			if (s.indexOf("I: 2") != -1)
//				break;
//		}
	}

	/***************************************************************************
	 * 获取好友列表
	 */	
	public function getContactList():void{
		var body:String = "<args><contacts version=\"6\"><buddy-lists /><buddies attributes=\"all\" /><mobile-buddies attributes=\"all\" /><chat-friends /><blacklist /></contacts></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: "+I_SHOWCONTANTLIST
				+ RETURN
				+ "Q: "+getQ()+" S"
				+ RETURN
				+ "N: GetContactList"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body
		send(data);
		
	}
//	S fetion.com.cn SIP-C/2.0
//F: 590256435
//I: 26 
//Q: 1 S
//N: GetContactsInfo
//L: 137
//
//<args><contacts attributes="all" extended-attributes="score-level"><contact uri="sip:560353887@fetion.com.cn;p=4511" /></contacts></args>

	public function getContactsInfo(fetionUri:String):void{
		var body:String="<args><contacts attributes=\"all\" extended-attributes=\"score-level\"><contact uri=\""
				+ fetionUri + "\" /></contacts></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: 40"
				+ RETURN
				+ "Q: "
				+ getQ()
				+ " S"
				+ RETURN
				+ "N: GetContactsInfo"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN

				+ body;

		send(data);

	}
	
	/***************************************************************************
	 * 修改密码
	 */	
	public function modifyPwd():void{
		
	}
	/***************************************************************************
	 * 修改用户信息
	 */	
	public function modifyUserInfo(nickname:String,impresa:String):void{
		var body:String = "<args><personal nickname=\""+nickname+"\" impresa=\""+impresa+"\" /></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: "+I_MODIFYUSER
				+ RETURN
				+ "Q: "+getQ()+" S"
				+ RETURN
				+ "N: SetPersonalInfo"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body
			
	  send(data);
	

	}
	
	/***************************************************************************
	 * 注册用户
	 */	
	public function regUser():void{
		
	}
	
	/***************************************************************************
	 * 忘记密码
	 */	
	public function forgetPwd():void{
		
	}
	
	/***************************************************************************
	 * 修改用户权限
	 */	
	public function modifyUserRight(userRightParam:String):void{

		var body:String = "<args><permissions values=\"buddy="+userRightParam+"\" /></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: "+I_MODIFYUSERRIGHT
				+ RETURN
				+ "Q: "+getQ()+" S"
				+ RETURN
				+ "N: SetGlobalPermission"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body
		send(data);
	}
	 /***************************************************************************
	 * 获取用户权限
	 */	
	public function getUserRight():void{
//S fetion.com.cn SIP-C/2.0
//F: 795757840
//I: 6 
//Q: 1 S
//N: GetGlobalPermission
//L: 42
//
//<args><permissions objects="all" /></args>
		var body:String = "<args><permissions objects=\"all\" /></args>";
		var data:String = "S fetion.com.cn SIP-C/2.0"
				+ RETURN
				+ "F: "
				+ fetionId
				+ RETURN
				+ "I: "+I_MODIFYUSERRIGHT
				+ RETURN
				+ "Q: 1 S"
				+ RETURN
				+ "N: GetGlobalPermission"
				+ RETURN
				+ "L: "+getLength(body)
				+ RETURN
				+ RETURN
				+ body
		send(data);
	}
	/***************************************************************************
	 * 接收信息
	 */	
	public function recMsg():void{
		
	}
	/***************************************************************************
	 * 用户退出
	 */	
	public function userQuit():void{
		
		var data:String ="<USERQUIT/>";
		send(data);			
		
	}
	/***************************************************************************
	 * 取得图形验证码--忘记密码
	 */	
		public function getPicCode():void{
//GET /nav/GetPicCode.aspx HTTP/1.1
//User-Agent: IIC2.0/PC 3.2.0540
//Host: 221.130.45.201
//Connection: Keep-Alive
	    	
			var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/PicCodeServlet?"+
			"urlId="+getUrlId();
                
            var loader:URLLoader = new URLLoader();
            this.configureGetPicCodeEventListeners(loader);  
                 
            var header1:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
            var header2:URLRequestHeader = new URLRequestHeader("cache-control", "no-cache");  
            var header3:URLRequestHeader = new URLRequestHeader("expires", "0");   
           
            var request:URLRequest = new URLRequest(url);        
         
           	request.requestHeaders.push(header1);  
           	request.requestHeaders.push(header2); 
            request.requestHeaders.push(header3);                         
            request.method = URLRequestMethod.GET;
            
            
            try{
                loader.load(request);            	
            }catch(error:Error){
            	trace(error.message);
            }
        }
//    		String xml="<result>";
//		xml+="<picId>"+picId+"</picId>";
//		xml+="<picContent>"+picContent+"</picContent>";		
//		xml+="</result>";    
        private function completeGetPicCodeHandler(event:Event):void
		{			 
			var loader:URLLoader = URLLoader(event.target);	
			trace(loader.data);
			var myXML:XML = XML(loader.data);
			
//			Alert.show(loader.data);
			
			picId=myXML.picId;
			picContent=myXML.picContent;
			
	//		Alert.show(picId);
			
			var pwdWindow:GetPwd=Application.application.getPwdWindow;
			pwdWindow.validateCode=picContent;
			
			
		}
		
   		public function configureGetPicCodeEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeGetPicCodeHandler);
        }
     /***************************************************************************
	 * 取得图形验证码--注册用户
	 */	
		public function getPicCodeR():void{
//GET /nav/GetPicCode.aspx HTTP/1.1
//User-Agent: IIC2.0/PC 3.2.0540
//Host: 221.130.45.201
//Connection: Keep-Alive
	    
		var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/PicCodeServlet?"+
			"urlId="+getUrlId();
                
            var loader:URLLoader = new URLLoader();
            this.configureGetPicCodeREventListeners(loader);  
                      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            try{
                loader.load(request);
            
            }catch(error:Error){
                trace(error.message);
            }
        }
//    		String xml="<result>";
//		xml+="<picId>"+picId+"</picId>";
//		xml+="<picContent>"+picContent+"</picContent>";		
//		xml+="</result>";    
        private function completeGetPicCodeRHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			trace(loader.data);
			var myXML:XML = XML(loader.data);
			
//			Alert.show(loader.data);
			
			picId=myXML.picId;
			picContent=myXML.picContent;
		
	//	Alert.show(picId);
		
			var addWindow:RegUser=Application.application.addWindow;
			addWindow.validateCode=picContent;
		
		}
		
   		public function configureGetPicCodeREventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeGetPicCodeRHandler);
        }
           
	
	/***************************************************************************
	 * 取得短信码
	 */	
		public function getSmsCode():void{

	    
		var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/SmsCodeServlet?"+
			"picId="+picId+"&picCode="+picCode+"&tmpMobile="+tmpMobile+"&urlId="+getUrlId();
                
            var loader:URLLoader = new URLLoader();
            this.configureGetSmsCodeEventListeners(loader);  
                      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            try{
                loader.load(request);
            
            }catch(error:Error){
                trace(error.message);
            }
        }
//		String picId=request.getParameter("picId");
//		String picCode=request.getParameter("picCode");
//		String tmpMobile=request.getParameter("tmpMobile");
		   
        private function completeGetSmsCodeHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			trace(loader.data);
			var result:String=String(loader.data);
			if(result=="ERROR"){
				Alert.show("验证码输入错误!","警告");
			}
			if(result=="LOCK"){
				Alert.show("帐户被锁定，请稍候再试！","警告");
			}
			
	
		}
		
   		public function configureGetSmsCodeEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeGetSmsCodeHandler);
        }
	
	/***************************************************************************
	 * 找回密码
	 */	
		public function getUpdatePwd():void{

	    
		var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/UpdataPwdServlet?"+
			"smsCode="+smsCode+"&newPassword="+newPassword+"&tmpMobile="+tmpMobile+"&urlId="+getUrlId();
                
            var loader:URLLoader = new URLLoader();
            this.configureUpdatePwdEventListeners(loader);  
                      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            try{
                loader.load(request);
            
            }catch(error:Error){
                trace(error.message);
            }
        }
//		String smsCode=request.getParameter("smsCode");
//		String newPassword=request.getParameter("newPassword");
//		String tmpMobile=request.getParameter("tmpMobile");
		   
        private function completeUpdatePwdHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			trace(loader.data);
	
		}
		
   		public function configureUpdatePwdEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeUpdatePwdHandler);
        }
        
        
        
        
        
	/***************************************************************************
	 * 注册用户
	 */	
		public function getRegUser():void{

	    
		var url:String = "http://"+SERVER_IP+":"+TOMCAT_PORT+"/fetion/servlet/RegUserServlet?"+
			"smsCode="+smsCode+"&newPassword="+newPassword+"&tmpMobile="+tmpMobile+"&urlId="+getUrlId();
                
            var loader:URLLoader = new URLLoader();
            this.configureRegUserEventListeners(loader);  
                      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.GET;
            try{
                loader.load(request);
            
            }catch(error:Error){
                trace(error.message);
            }
        }
//		String smsCode=request.getParameter("smsCode");
//		String newPassword=request.getParameter("newPassword");
//		String tmpMobile=request.getParameter("tmpMobile");
		   
        private function completeRegUserHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);	
			trace(loader.data);
			var myXML:XML = XML(loader.data);
		
			var addWindow:RegUser=Application.application.addWindow;
			addWindow.fetionIdReg=myXML.feteionId;
		}
		
   		public function configureRegUserEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeRegUserHandler);
        }
	
	
	/***************************************************************************
	 * 获取好友请求
	 */	
	public function recFriendRequest():void{
		Application.application.showFriendRequestCanvas();
	}
	
	/***************************************************************************
	 * 保持连接的心跳消息
	 * 	public void keepAlive() {
		try {
			String data = "R fetion.com.cn SIP-C/2.0" + RETURN + "F: "
					+ fetionId + RETURN + "I: 1 "+ RETURN + "Q: " + getRQ()
					+ " R"+ RETURN+ RETURN;
			write(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 */	
//	public function keepAlive():void{
//		var data:String = "R fetion.com.cn SIP-C/2.0" 
//				+ RETURN 
//				+ "F: " + fetionId
//				+ RETURN 
//				+ "I: 1 " 
//				+ RETURN 
//				+ "Q: "+getSeqKeep()+" R"+ RETURN+RETURN;
//
//		send(data);
//	}
	public function keepAlive():void{	
		var data:String="<KEEPALIVE/>";
		send(data);
	}
	//SIP-C/2.0 200 OK
//T: sip:612612272@fetion.com.cn;p=3517
//I: 3
//Q: 1 I
//K: text/plain
//K: multiparty
//好友打开窗口
//好友来消息回复
//I 837204993 SIP-C/2.0
//F: sip:612612272@fetion.com.cn;p=3517
//I: -18
//K: text/plain
//K: text/html-fragment
//K: multiparty
//K: nudge
//K: share-background
//Q: 19 I
//L: 21
//
//s=session
//m=message 
//好友来消息回复
//SIP-C/2.0 200 OK
//T: sip:612612272@fetion.com.cn;p=3517
//I: 3
//Q: 1 I
//K: text/plain
//K: multiparty
	/***************************************************************************
	 * 回复好友消息信息
	 * 
	 */	
//SIP-C/2.0 200 OK
//T: sip:612612272@fetion.com.cn;p=3517
//I: -30
//K: text/plain
//K: text/html-fragment
//K: multiparty
//K: nudge
//Q: 31 I
//L: 135
//
//v=0
//o=-0 0 IN 192.168.0.10 9876
//c=IN IP4 192.168.0.10 9876
//s=session 
//t: 0 0
//m=message 9876 sip sip:837204993@fetion.com.cn;p=5907



//	 SIP-C/2.0 200 OK
//Q: 1 I
//K: text/html-fragment
//K: multiparty
//T: sip:123456789@fetion.com.cn;p=xxx
//I: 16
//L: 135
//
//v=0
//o=-0 0 IN 222.222.222.222:2222
//s=session
//c=IN IP4 222.222.222.222:2222
//t=0 0
//m=message 2222 sip sip:987654321@fetion.com.cn;p=xxx



//v=0..
//o=-0 0 IN 127.0.0.1:3731..
//s=session..
//c=IN IP4 127.0.0.1:3731.
//t=0 0..
//m=message 3731 sip sip:612612272@fetion.com.cn;p=3517..
	public function responseInvite(itemUri:String,itemI:String,itemQ:String):void{
		
		var body:String =
				"v=0"
				+ RETURN 
				+ "o=-0 0 IN 127.0.0.1:3731"
				+ RETURN
				+ "s=session"
				+ RETURN
				+ "c=IN IP4 127.0.0.1:3731"			
				+ RETURN
				+ "t=0 0" + RETURN
				+ "m=message 3731 sip "+fetionUri;
//				+ RETURN;		
//SIP-C/2.0 200 OK.
//I: -195 ..
//Q: 196 I.
//.F: sip:837204993@fetion.com.cn;p=5907..K: 
//text/plain..
//K: multiparty.
//.L: 129....		
		var data:String = "SIP-C/2.0 200 OK" 
				+ RETURN 
				+ "F: " + itemUri
				+ RETURN 
				+ "I: "+itemI 
				+ RETURN
				+"Q: "+itemQ
				+ RETURN
				+"K: text/plain"
				+ RETURN
				+"K: multiparty"			
				+ RETURN 
				+ "L: "+getLength(body) 
				+ RETURN+ RETURN
				+ body;

		send(data);
	}
//加好友结果
//BN 837204993 SIP-C/2.0
//I: 1
//N: contact
//Q: 7 BN
//L: 180
//
//<events>
//<event type="UpdateBuddy">
//<contacts version="294832668">
//<buddies>
//<buddy uri="sip:612612272@fetion.com.cn;p=3517" relation-status="2"/>
//</buddies></contacts></event></events>
//	

	
	
	public function getLength(str:String):int {
			var mbyte:ByteArray=new ByteArray();
			mbyte.writeUTFBytes(str);
			return mbyte.length;
	}

	 
    public function requestPicCodePwd():void{
		getPicCode();
    }
 
    
    public function requestPicCodeReg():void{
		getPicCodeR();
    }


	public function requestRegUser():void{
		getRegUser();

    }
    
    public function requestSmsCode():void{
		getSmsCode();

    }

	public function requestUpdatePwd():void{
		getUpdatePwd();
	}
 }
}